﻿
using BLL;
using Common;
using IBLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using user_defined_Panl;

namespace WindowsFormsApp1
{
   
    public partial class Form2 : Form
    {
        IFrQuestionBankManage_IBLL frQuestion = new FrQuestionBankManage_BLL();
        IFrExamManage_IBLL frExam = new FrExamManage_BLL();
           List<List<string>> result = new List<List<string>>();
        string s="", o="";
        public Form2(string id,string examid)
        {
            InitializeComponent();
            s = id;
            o = examid;
            user(id);
            k(id,examid);
            int index = 0;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("exec sys_SC_Eme 8;");
            stringBuilder.Append("exec sys_MC_Eme 8;");
            stringBuilder.Append("exec sys_TOF_Eme 8;");
            stringBuilder.Append("exec sys_CP_Eme 8;");
            SqlDataReader sqlData = frQuestion.Query(stringBuilder.ToString());
            if (sqlData != null)
            {
                while (sqlData.Read())
                {
                    Panl_1 panl_1 = new Panl_1(flowLayoutPanel1);

                    flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[0].Text = sqlData["Topic"].ToString();
                    flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[1].Text = sqlData["A"].ToString();
                    flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[2].Text = sqlData["B"].ToString();
                    flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[3].Text = sqlData["C"].ToString();
                    flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[4].Text = sqlData["D"].ToString();
                    if (result[0][index] != "" && result[0][index] != null)
                    {
                      
                        switch (result[0][index])
                        {

                            case "A":
                                (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[5] as RadioButton).Checked = true; break;
                            case "B":
                                (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[6] as RadioButton).Checked = true; break;
                            case "C":
                                (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[7] as RadioButton).Checked = true; break;
                            case "D":
                                (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[8] as RadioButton).Checked = true; break;
                            default:
                                break;
                        }
                    }
                    flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[0].Text = flowLayoutPanel1.Controls.Count.ToString();

                    index++;
                }
                if (sqlData.NextResult())
                {
                    index = 0;
                    while (sqlData.Read())
                    {
                        Panl_2 panl_2 = new Panl_2(flowLayoutPanel1);
                        char[] y = sqlData["Answer"].ToString().ToCharArray();
   
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[0].Text = sqlData["Topic"].ToString();
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[1].Text = sqlData["A"].ToString();
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[2].Text = sqlData["B"].ToString();
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[3].Text = sqlData["C"].ToString();
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[4].Text = sqlData["D"].ToString();
                        if (result[1][index]!=""&& result[1][index]!=null)
                        {
                            for (int i = 0; i < result[1][index].Length; i++)
                            {
                                
                                switch (result[1][index][i])
                                {

                                    case 'A': 
                                        (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[5] as CheckBox).Checked=true; break;
                                    case 'B':
                                        (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[6] as CheckBox).Checked = true; break;
                                    case 'C':
                                        (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[7] as CheckBox).Checked = true; break;
                                    case 'D':
                                        (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[8] as CheckBox).Checked = true; break;
                                    default:
                                        break;
                                }
                            }
                        }
                       
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[0].Text = flowLayoutPanel1.Controls.Count.ToString();
                        index++;
                    }
                }
                if (sqlData.NextResult())
                {
                    index = 0;
                    while (sqlData.Read())
                    {
                        Panl_3 panl_3 = new Panl_3(flowLayoutPanel1);
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[0].Text = sqlData["Topic"].ToString();
                        if (result[2][index] != "" && result[2][index] != null)
                        {
                            if ((flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[1] as RadioButton).Text == result[2][index])
                            {
                                (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[1] as RadioButton).Checked = true;
                            }
                            else
                            {
                                (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[2] as RadioButton).Checked = true;
                            }
                        }
                        
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[0].Text = flowLayoutPanel1.Controls.Count.ToString();
                        index++;
                    }
                }
                if (sqlData.NextResult())
                {
                    index = 0;
                    while (sqlData.Read())
                    {
                        Panl_4 panl_4 = new Panl_4(flowLayoutPanel1);
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[0].Text = sqlData["Topic"].ToString();
                        (flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[1].Controls[1] as TextBox).Text = result[3][index];
                        flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Controls[0].Text = flowLayoutPanel1.Controls.Count.ToString();
                        index++;
                    }
                }
               
                t = true;
            }
            sqlData.Close();
        }
        public void user(string id)
        {
            SqlDataReader reader2 = frExam.Check($"	select StudentName,StuImage from view_1 where StudentId ='{id}';" +
                $"select *from Score where Exam_id='{o}' and StudentId='{id}';");
            if (reader2.Read())
            {
                label9.Text = reader2[0].ToString();
                label10.Text = id;
                pictureBox1.Image = reader2[1].ToString() != null && reader2[0].ToString() != "" ? (Image)Common_Com.DeSerializeObjevtToString(reader2[1].ToString()) : Image.FromFile("./pfltmjdoprx.jpg");
            }
            if (reader2.NextResult())
            {
                textBox1.Text ="0";
              textBox2.Text   ="0";
              textBox3.Text   ="0";
                textBox4.Text = "0";
                if (reader2.Read())
                {
                    textBox1.Text = reader2[2].ToString() ??"0";
                    textBox2.Text = reader2[3].ToString()??"0";
                    textBox3.Text = reader2[4].ToString()??"0";
                    textBox4.Text = reader2[5].ToString()??"0";
                }
            }

        }
        public void k(string id, string examid)
        {
            StringBuilder path = new StringBuilder();
            path.Append($"select * from SingleChoice_Examinee  WHERE Exam_id='{examid}' and StudentId='{id}';");
            path.Append($"select * from MultipleChoice_Examinee  WHERE Exam_id='{examid}' and StudentId='{id}';");
            path.Append($"select * from TrueOrFalseQuestions_Examinee  WHERE Exam_id='{examid}' and StudentId='{id}';");
            path.Append($"select * from Completion_Examinee  WHERE Exam_id='{examid}' and StudentId='{id}';");
            SqlDataReader reader2 = frExam.Check(path.ToString()); ;
            List<string> Y = new List<string>();

            if (reader2.Read())
            {
                for (int i = 2; i < 12; i++)
                {
                    Y.Add(reader2[i].ToString());
                }
               
            }
            result.Add(Y);
            Y = new List<string>();
            if (reader2.NextResult())
            {
                if (reader2.Read())
                {
                    for (int i = 2; i < 12; i++)
                    {
                        Y.Add(reader2[i].ToString());
                    }

                }
            }
            result.Add(Y);
            Y = new List<string>();
            if (reader2.NextResult())
            {
                if (reader2.Read())
                {
                    for (int i = 2; i < 12; i++)
                    {
                        Y.Add(reader2[i].ToString());
                    }

                }
            }
            result.Add(Y);
            Y = new List<string>();
            if (reader2.NextResult())
            {
                if (reader2.Read())
                {
                    for (int i = 2; i < 12; i++)
                    {
                        Y.Add(reader2[i].ToString());
                    }
                }
            }
            result.Add(Y);
            reader2.Close();
        }
        bool t = false;
        private void textBox4_TextChanged(object sender, EventArgs e)
        {

            if (!t)
            {
                return;
            }
            TextBox text = (TextBox)sender;
            if (text.Text==null||text.Text=="")
            {
                text.Text = "0";
            }
            if (!int.TryParse(text.Text,out int x))
            {
                text.Text = "0";
                MessageBox.Show("请输入正确的分数");
                return;
            }
            label6.Text =(Convert.ToInt32( textBox1.Text)+ Convert.ToInt32(textBox2.Text)+ Convert.ToInt32(textBox3.Text)+ Convert.ToInt32(textBox4.Text)).ToString();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            try
            {
                frQuestion.SubMit($"insert into Score values({o},{s},{textBox1.Text},{textBox2.Text},{textBox3.Text},{textBox4.Text});");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存分数失败 错误信息:"+ex.Message);

            }
           
        }
    }
}
